//
//  NetworkTool.swift
//  CJXSwiftStoryboard
//
//  Created by CJX on 2016/10/19.
//  Copyright © 2016年 caijianxi. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestType {
    case GET
    case POST
}

class NetworkTool: AFHTTPSessionManager {
    
    static let shareInstance : NetworkTool = {
        let tool = NetworkTool()
//        tool.responseSerializer.acceptableContentTypes?.insert("text/html")
//        tool.responseSerializer.acceptableContentTypes?.insert("image/jpeg")
        tool.responseSerializer.acceptableContentTypes = NSSet(objects: "text/html","image/jpeg") as! Set<String>
        return tool
    }()
    
}


extension NetworkTool{
    func request(requestType:RequestType,urlString:String,parmas:[String:NSObject],finishedBlock : (result:AnyObject?,error:NSError?)->())->(){
        let successBlock = {
            (task:NSURLSessionDataTask,result:AnyObject?)in finishedBlock(result: result, error: nil)
        }
        let failureBlock = {
            (task:NSURLSessionDataTask?,error:NSError) in finishedBlock(result: nil, error: error)
        }
        
        if requestType == .GET {
            GET(urlString, parameters: parmas, progress: nil, success: successBlock, failure: failureBlock)
        }else{
            POST(urlString, parameters: parmas, progress: nil, success: successBlock, failure: failureBlock)
        }
        
    }
}
